/*	Interactive Gravitational Simulator
	Project: IGS Core
	File: NBodySystem.h
	Author: Mike Bantegui <mbante2@gmail.com>, Hofstra University 
	Copyright (C) 2012 - 2013 Mike Bantegui

	This file is part of the IGS software package for simulating
	the N-Body problem in real-time. This was developed as part
	of a departmental honors thesis project at Hofstra University
	in the Spring 2012 semester.

	IGS is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	IGS is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef IGS_NBODYSYSTEM_H
#define IGS_NBODYSYSTEM_H

#include "Dynamics.h"
#include <vector>

namespace IGS
{

struct Body;
class Snapshot;
struct ViewNode;

// Represents a system of N mutually gravitating bodies.
class NBodySystem
{
private:
	// The bodies in this system. NBodySystem takes ownership of this
	Body *Bodies;
	// List of pointers to bodies in the system
	std::vector<Body *> BodyPointers;
	// The size of the system
	const int N;
	// The total mass of the system
	const double _Mass;
	// The softening length of the system
	double _Softening;
	// The squared softening length of the system
	double _SofteningSquared;
	// Whether the system needs to update energies
	bool _UpdateEnergy;
	// Whether the system needs to update the barycenter
	bool _UpdateBarycenter;
	// Whether the system needs to update the bounds
	bool _UpdateBounds;
	// Whether the system needs to update the maximal quantities
	bool _UpdateMaxima;
	// The dynamical properties of this system
	Dynamics _Dynamics;
	// The tree structure for this system
	mutable std::shared_ptr<ViewNode> _Tree;

	void UpdateEnergy();
	void UpdateBarycenter();
	void UpdateBounds();
	void UpdateMaxima();

public:
	NBodySystem(Body *bodies, const int n);
	~NBodySystem();

	void Update();
	void RequireUpdate();
	void ZeroBarycenter();

	void AttachTree(std::shared_ptr<ViewNode> tree);
	std::shared_ptr<ViewNode> Tree() const;

	const double KineticEnergy();
	const double KineticEnergy() const;
	const double PotentialEnergy();
	const double PotentialEnergy() const;
	const double TotalEnergy();
	const double TotalEnergy() const;

	const double MaxDistance();
	const double MaxDistance() const;
	const double MaxVelocity();
	const double MaxVelocity() const;
	const double MaxMomentum();
	const double MaxMomentum() const;

	const double MaxMass() const;

	const Vector CenterOfMass();
	const Vector CenterOfMass() const;
	const Vector CenterOfMomentum();
	const Vector CenterOfMomentum() const;

	const Vector AngularMomentum();
	const Vector AngularMomentum() const;
	const Vector AngularVelocity();
	const Vector AngularVelocity() const;
	const Vector TotalForce();
	const Vector TotalForce() const;

	const Vector MinBound();
	const Vector MinBound() const;
	const Vector MaxBound();
	const Vector MaxBound() const;

	const int Size() const;
	const double Mass() const;

	const double &SofteningSquared() const;
	const double &Softening() const;

	void SetSoftening(const double softening);

	const Body &operator[](const int i) const;
	Body &operator[](const int i);
	std::vector<Body *> &Pointers();

	Snapshot *ToSnapshot() const;

	static NBodySystem *Read(const char *filename);
	static bool Write(const NBodySystem &system, const char *filename);
};

}

#endif
